﻿/*****************************************介绍*****************************************
 * 作者：User
 * 创建时间：2025-09-08 13:48:23
 * 功能：界面【ChoosePanel】的View
        1、负责收集所有控件
**************************************************************************************/

using Game.Framework;
using System;

namespace Game.UI.ChoosePanel
{
    public class ChoosePanelView : PopupPanel
    {
        public const string PANEL_KEY = "Game_UI_ChoosePanel_ChoosePanel_Key";
        private Item[] m_Items;
        private GameDataModel.Level m_Level;

        protected override void RegistAny()
        {
            PanelManager.Instance.RegistPanel(PANEL_KEY, this);
        }

        protected override void UnregistAny()
        {
            PanelManager.Instance.UnregistPanel(PANEL_KEY);
        }

        protected override void InitUI()
        {
            m_Items = transform.Find("Bg").GetComponentsInChildren<Item>();
        }

        public override void Init(object data)
        {
            m_Level = data as GameDataModel.Level;
            for (int i = 0; i < m_Items.Length; i++)
                m_Items[i].Init(m_Level, i, this);

            if (m_Level.assetsInfo == null)
            {
                AssetCache.CacheSmallImgs(m_Level, () =>
                {
                    for (int i = 0; i < m_Items.Length; i++)
                    {
                        m_Items[i].Init(m_Level, i, this);
                    }
                });
            }
        }

        protected override void ClickBtnHandle(string name)
        {
            UIUtility.HidePanel(PANEL_KEY);
        }

        public override void Hide(bool isDestroy = false)
        {
            for (int i = 0; i < m_Items.Length; i++)
                m_Items[i].Clear();
            base.Hide(isDestroy);
        }

        // 使用免费刷新成功，这里更新其他Item的状态
        public void FreeRefreshSucc(int idx)
        {
            for (int i = 0; i < m_Items.Length; i++)
            {
                if (i == idx) continue;
                m_Items[i].RefreshBtnUpdate();
            }
        }
    }
}